package libase.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import libase.web.DateFormat;

/**
 *
 * @author aspr0
 */
@Entity
@Table (name = "BOOK")
@XmlRootElement
@NamedQueries (
        {
    @NamedQuery (name = "Book.findAll", query = "SELECT b FROM Book b order by b.id"),
    @NamedQuery (name = "Book.findById", query = "SELECT b FROM Book b WHERE b.id = :id"),
    @NamedQuery (name = "Book.findByName", query = "SELECT b FROM Book b WHERE b.name = :name"),
    @NamedQuery (name = "Book.findByPubDt", query = "SELECT b FROM Book b WHERE b.pubDt = :pubDt")
})
public class Book implements Serializable
{
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @SequenceGenerator (allocationSize = 1, name = "sbg", sequenceName = "SEQ_BOOK")
    @GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "sbg")
    @Id
    @Basic (optional = false)
    @NotNull
    @Column (name = "ID")
    private BigDecimal id;
    @Basic (optional = false)
    @NotNull
    @Size (min = 1, max = 200)
    @Column (name = "NAME")
    private String name;
    @Basic (optional = false)
    @NotNull
    @Column (name = "PUB_DT")
    @Temporal (TemporalType.DATE)
    private Date pubDt;
    @JoinColumn (name = "PUB_ID", referencedColumnName = "ID")
    @ManyToOne (optional = false)
    private Publisher pub;
    @JoinColumn (name = "AUTH_ID", referencedColumnName = "ID")
    @ManyToOne (optional = false)
    private Author auth;
    @OneToMany (cascade = CascadeType.ALL, mappedBy = "book")
    private Collection<Exemplar> exemplars;

    public Book()
    {
    }

    public Book(BigDecimal id)
    {
        this.id = id;
    }

    public Book(BigDecimal id, String name, Date pubDt)
    {
        this.id = id;
        this.name = name;
        this.pubDt = pubDt;
    }

    public BigDecimal getId()
    {
        return id;
    }

    public void setId(BigDecimal id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Date getPubDt()
    {
        return pubDt;
    }

    public void setPubDt(Date pubDt)
    {
        this.pubDt = pubDt;
    }

    public Publisher getPub()
    {
        return pub;
    }

    public void setPub(Publisher pub)
    {
        this.pub = pub;
    }

    public Author getAuth()
    {
        return auth;
    }

    public void setAuth(Author auth)
    {
        this.auth = auth;
    }

    @XmlTransient
    public Collection<Exemplar> getExemplars()
    {
        return exemplars;
    }

    public void setExemplars(
            Collection<Exemplar> exemplarCollection)
    {
        this.exemplars = exemplarCollection;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Book))
        {
            return false;
        }
        Book other = (Book) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return (id == null)
                ? "unknown book"
                : String.format("%s.%s. %s «%s» %s",
                                auth.getFirstNm().substring(0, 1),
                                (auth.getMiddleNm() == null) ? "" : auth.getMiddleNm().substring(0, 1),
                                auth.getLastNm(),
                                name,
                                DateFormat.asY(pubDt));
    }
}
